package com.spring.housing.configurations;

import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


import javax.sql.DataSource;
import java.util.HashMap;

@Slf4j
@Configuration
@ConditionalOnMissingBean(name = "dataSource")

@EnableTransactionManagement
public class JdbcConfig {
    public static final String PRE="spring.datasource.db1";

//配置数据库

    @Bean
    @ConfigurationProperties(prefix = PRE)
    public DataSource dataSource() {
        log.info("数据库初始化成功!{}", PRE);
        return DataSourceBuilder.create().build();
    }


    @Bean
    public JdbcTemplate Db1Template(@Qualifier("dataSource") DataSource dataSource){
        log.info("init ectJdbcTemplate success!!!");
        log.info("hikariDataSource.getMaximumPoolSize() is {}",((HikariDataSource)dataSource).getMaximumPoolSize());
        log.info("hikariDataSource.getMinimumIdle() is {}",((HikariDataSource)dataSource).getMinimumIdle());
        log.info("hikariDataSource.getPoolName() is {}",((HikariDataSource)dataSource).getPoolName());
        log.info("hikariDataSource.getMaxLifetime() is {}",((HikariDataSource)dataSource).getMaxLifetime());
        return new JdbcTemplate(dataSource);
    }



}
